import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

/// 底部导航
class UiBottomNavigationBarItem {
  Widget? child;
  String? label;
  dynamic value;
  Widget Function(UiBottomNavigationBar)? builder;
  UiBottomNavigationBarItem({
    this.child,
    this.builder,
    this.label,
    this.value,
  });
}

class UiBottomNavigationBar extends StatelessWidget {
  const UiBottomNavigationBar({
    super.key,
    required this.items,
    this.onTap,
    this.currentIndex = 0,
    this.backgroundColor,
    this.foregroundColor = Colors.white,
    this.labelSize,
    this.elevation = 0,
    this.badges = const {},
    this.badgeColor = Colors.pink,
  });
  final List<UiBottomNavigationBarItem> items;
  final Function(dynamic value)? onTap;
  final int currentIndex;
  final Color? backgroundColor;
  final Color foregroundColor;
  final double? labelSize;
  final double elevation;
  final Map<int, dynamic> badges;
  final Color badgeColor;
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData(
        iconTheme: IconThemeData(color: foregroundColor),
        primaryColorDark: Colors.red,
        primaryColor: Colors.red,
        primaryColorLight: Colors.blue,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (elevation > 0)
            Divider(
              height: 0.3,
              thickness: elevation,
            ),
          Container(
            color: backgroundColor,
            child: SafeArea(
              bottom: true,
              top: false,
              child: DefaultTextStyle(
                style: TextStyle(
                  color: foregroundColor.withOpacity(0.55),
                  fontSize: labelSize,
                  fontFamily: UiTheme.fontFamily,
                  fontWeight: FontWeight.w500,
                ),
                child: SizedBox(
                  height: kBottomNavigationBarHeight,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: List.generate(items.length, (index) {
                      UiBottomNavigationBarItem item = items[index];
                      var value = item.value ?? index;
                      return GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          onTap?.call(value);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width:
                              MediaQuery.of(context).size.width / items.length,
                          child: UiBadge(
                            color: badgeColor,
                            content: Text('${badges[index]}'),
                            show: badges[index] != null && badges[index] != '',
                            type:
                                badges[index] == 'new' ? UiBadgeType.dot : null,
                            child: item.builder?.call(this) ??
                                item.child ??
                                Text(
                                  item.label.toString(),
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: currentIndex == value
                                        ? foregroundColor
                                        : null,
                                  ),
                                ),
                          ),
                        ),
                      );
                    }),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
